package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.NavDrawerItem;

public class NavDrawerListAdapter extends BaseExpandableListAdapter {

	private ArrayList<NavDrawerItem> parentRecord; // list of navigation drawer
													// items
	private HashMap<String, List<String>> childRecord;
	private LayoutInflater inflater = null;
	private Activity mContext;

	public NavDrawerListAdapter(Activity context,
			ArrayList<NavDrawerItem> parentList,
			HashMap<String, List<String>> childList) {
		this.parentRecord = parentList;
		this.childRecord = childList;
		mContext = context;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public String getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return this.childRecord.get(
				((NavDrawerItem) getGroup(groupPosition)).getTitle()).get(
				childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String childConfig = getChild(groupPosition, childPosition);

		ViewHolder holder;
		try {
			if (convertView == null) {
				holder = new ViewHolder();

				convertView = inflater.inflate(
						R.layout.slide_menu_list_item_child, null);
				holder.childTitle = (TextView) convertView
						.findViewById(R.id.childTitle);
				holder.childIcon = (ImageView) convertView
						.findViewById(R.id.childIcon);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.childTitle.setText(childConfig);
			// holder.childTitle.setTextColor(Color.WHITE);

			switch (childConfig) {
			case "Foods":
				holder.childIcon.setImageResource(R.drawable.ic_food_d);
				break;
			case "Transportation":
				holder.childIcon
						.setImageResource(R.drawable.ic_transportation_d);
				break;
			case "Holidays":
				holder.childIcon.setImageResource(R.drawable.ic_holiday_d);
				break;
			case "Common Dialogues":
				holder.childIcon.setImageResource(R.drawable.ic_dialogue_d);
				break;

			default:
				break;
			}

		} catch (Exception e) {
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.childRecord.get(
				((NavDrawerItem) getGroup(groupPosition)).getTitle()).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return this.parentRecord.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.parentRecord.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		NavDrawerItem parentNavDrawerItem = parentRecord.get(groupPosition);

		ViewHolder holder;
		try {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.slide_menu_list_item,
						null);
				holder = new ViewHolder();

				holder.parentTitle = (TextView) convertView
						.findViewById(R.id.title);
				holder.parentIcon = (ImageView) convertView
						.findViewById(R.id.icon);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.parentTitle.setText(parentNavDrawerItem.getTitle());
			holder.parentIcon.setBackgroundResource(parentNavDrawerItem
					.getIcon());
			holder.parentIcon.setImageResource(parentRecord.get(groupPosition)
					.getIcon());

		} catch (Exception e) {
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public static class ViewHolder {

		private TextView childTitle;
		private ImageView childIcon;
		// Content
		private TextView parentTitle;
		private ImageView parentIcon;

	}
	// private Context context;
	// private ArrayList<NavDrawerItem> navDrawerItems;
	//
	// public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem>
	// navDrawerItems){
	// this.context = context;
	// this.navDrawerItems = navDrawerItems;
	// }
	//
	// @Override
	// public int getCount() {
	// return navDrawerItems.size();
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// return navDrawerItems.get(position);
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// return position;
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	// if (convertView == null) {
	// LayoutInflater mInflater = (LayoutInflater)
	// context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	// convertView = mInflater.inflate(R.layout.drawer_list_item, null);
	// }
	//
	// ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
	// TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
	// // TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
	//
	// imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
	// txtTitle.setText(navDrawerItems.get(position).getTitle());
	//
	// // displaying count
	// // check whether it set visible or not
	// // if(navDrawerItems.get(position).getCounterVisibility()){
	// // txtCount.setText(navDrawerItems.get(position).getCount());
	// // }else{
	// // // hide the counter view
	// // txtCount.setVisibility(View.GONE);
	// // }
	//
	// return convertView;
	// }

}
